﻿using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Terraria.Social;

namespace Terraria
{
	// Token: 0x0200005C RID: 92
	public static class WindowsLaunch
	{
		// Token: 0x060007BF RID: 1983 RVA: 0x00371914 File Offset: 0x0036FB14
		private static bool ConsoleCtrlCheck(WindowsLaunch.CtrlTypes ctrlType)
		{
			bool flag = false;
			switch (ctrlType)
			{
			case WindowsLaunch.CtrlTypes.CTRL_C_EVENT:
				flag = true;
				break;
			case WindowsLaunch.CtrlTypes.CTRL_BREAK_EVENT:
				flag = true;
				break;
			case WindowsLaunch.CtrlTypes.CTRL_CLOSE_EVENT:
				flag = true;
				break;
			case WindowsLaunch.CtrlTypes.CTRL_LOGOFF_EVENT:
			case WindowsLaunch.CtrlTypes.CTRL_SHUTDOWN_EVENT:
				flag = true;
				break;
			}
			if (flag)
			{
				SocialAPI.Shutdown();
			}
			return true;
		}

		// Token: 0x060007C0 RID: 1984
		[DllImport("Kernel32")]
		public static extern bool SetConsoleCtrlHandler(WindowsLaunch.HandlerRoutine Handler, bool Add);

		// Token: 0x060007C1 RID: 1985 RVA: 0x0000A27F File Offset: 0x0000847F
		[STAThread]
		private static void Main(string[] args)
		{
			AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs sargs)
			{
				string resourceName = new AssemblyName(sargs.Name).Name + ".dll";
				string text = Array.Find<string>(typeof(Program).Assembly.GetManifestResourceNames(), (string element) => element.EndsWith(resourceName));
				if (text == null)
				{
					return null;
				}
				Assembly result;
				using (Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(text))
				{
					byte[] array = new byte[manifestResourceStream.Length];
					manifestResourceStream.Read(array, 0, array.Length);
					result = Assembly.Load(array);
				}
				return result;
			};
			Program.LaunchGame(args, false);
		}

		// Token: 0x04000D36 RID: 3382
		private static WindowsLaunch.HandlerRoutine _handleRoutine;

		// Token: 0x0200005D RID: 93
		// (Invoke) Token: 0x060007C3 RID: 1987
		public delegate bool HandlerRoutine(WindowsLaunch.CtrlTypes CtrlType);

		// Token: 0x0200005E RID: 94
		public enum CtrlTypes
		{
			// Token: 0x04000D38 RID: 3384
			CTRL_C_EVENT,
			// Token: 0x04000D39 RID: 3385
			CTRL_BREAK_EVENT,
			// Token: 0x04000D3A RID: 3386
			CTRL_CLOSE_EVENT,
			// Token: 0x04000D3B RID: 3387
			CTRL_LOGOFF_EVENT = 5,
			// Token: 0x04000D3C RID: 3388
			CTRL_SHUTDOWN_EVENT
		}
	}
}
